home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / MacTCP class library / MacTCP class headers / CTCP.h < prev    next >
Text File  |  1992-10-26  |  17KB  |  512 lines

  1. /*
  2.  
  3.   CTCP.h
  4.   Superclass:    CMacTCPDriver
  5.  
  6.      The TCP implementation (Chapter 4, MacTCP Programmer's Guide).
  7.  
  8.   Copyright © NCSA, University of Illinois;    June 2, 1992
  9.   Eric Johnson, John Newlin and Igor Livshits
  10.   
  11.   This code may be used, modified, and distributed free of charge and obligation.
  12.   
  13. */
  14.  
  15. #define    _H_CTCP                                                        //    Include this file only once
  16.  
  17. #include    "CMacTCPDriver.h"                                //    Superclass definitions
  18.  
  19. #ifndef __TYPES__
  20. #include    <Types.h>
  21. #endif
  22.  
  23. #define NumOfHistoBuckets        7                            //    Number of histogram buckets
  24. #define TCPRcvBfrReturn            36                        //    Overwrites TCPBfrReturn (only in Chapter 4)
  25. #define TCPCtlMax                        49                        //    TCP specific (only in Chapter 4)
  26. #define ipctlGetAddr                15                        //    Command code to get local IP address
  27. #define BYTES_16WORD               2                            //    Bytes per 16 bit ip word
  28. #define BYTES_32WORD            4                            //    Bytes per 32 bit ip word
  29. #define BYTES_64WORD            8                            //    Bbytes per 64 bit ip word
  30.  
  31. #define    kNameLength                    50                        //    Default name length
  32. #define    kinDataLength                1024                    //    Default input data length
  33. #define    kOutDataLength            1024                    //    Default output data length
  34. #define    kStreamLength              8192                    //    Minimum stream length
  35. #define    kDefaultFlags                0xc0                    //    Default validity flags
  36. #define    kDefaultAction            0x01                    //    Default timout action
  37. #define    kEOL                                0x00                    //    End of line marker
  38.  
  39. typedef enum TCPEventCode
  40. {                                                                                    //    TCP events (p.48)
  41.     TCPClosing= 1,
  42.     TCPULPTimeout,
  43.     TCPTerminate,
  44.     TCPDataArrival,
  45.     TCPUrgent,
  46.     TCPICMPReceived,
  47.     lastEvent= 65535
  48. };
  49.  
  50. typedef enum TCPTerminationReason
  51. {                                                                                    //    TCP termination reasons (p.49)
  52.     TCPRemoteAbort= 2,
  53.     TCPNetworkFailure,
  54.     TCPSecPrecMismatch,
  55.     TCPULPTimeoutTerminate,
  56.     TCPULPAbort,
  57.     TCPULPClose,
  58.     TCPServiceError,
  59.     lastReason= 65535
  60. };
  61.  
  62. typedef struct ip_addrbytes
  63. {                                                                                    //    Accommodates numeric and string-type addresses
  64.     union
  65.     {
  66.         b_32 addr;
  67.         char byte[4];
  68.     } a;
  69. } ip_addrbytes;
  70.     
  71. typedef struct wdsEntry
  72. {                                                                                    //    
  73.     unsigned short    length;                                        //    Length of buffer
  74.     char*                        ptr;                                            //    Pointer to buffer
  75. } wdsEntry;
  76.  
  77. typedef struct rdsEntry
  78. {
  79.     unsigned short    length;                                        //    Length of buffer
  80.     char*                        ptr;                                            //    Pointer to buffer
  81. } rdsEntry;
  82.  
  83. typedef unsigned long BufferPtr;                        //    Our buffer
  84. typedef unsigned long StreamPtr;                        //    Our stream
  85.  
  86. typedef enum ICMPMsgType 
  87. {                                                                                        //    Responses
  88.     netUnreach,
  89.     hostUnreach,
  90.     protocolUnreach,
  91.     portUnreach,
  92.     fragReqd,
  93.     sourceRouteFailed,
  94.     timeExceeded,
  95.     parmProblem,
  96.     missingOption,
  97.     lastICMPMsgType= 65535
  98. } ICMPMsgType;
  99.     
  100. typedef struct ICMPReport
  101. {                                                                                        //    Connection report
  102.     StreamPtr                    streamPtr;
  103.     ip_addr                        localHost;
  104.     ip_port                     localPort;
  105.     ip_addr                     remoteHost;
  106.     ip_port                     remotePort;
  107.     enum ICMPMsgType    reportType;
  108.     unsigned short        optionalAddlInfo;
  109.     unsigned long         optionalAddlInfoPtr;
  110. } ICMPReport;
  111.     
  112.  
  113. typedef pascal void (*TCPNotifyProc)            //    Notification routine
  114.             (    StreamPtr                        tcpStream,            //    Our stream
  115.                 unsigned short            eventCode,            //    Which event
  116.                 Ptr                                 userDataPtr,        //    Custom data
  117.                 unsigned short            terminReason,        //    Terminatioon code
  118.                 struct ICMPReport*    icmpMsg                    //    Status report
  119.             );
  120. typedef void (*TCPIOCompletionProc)                //    Completion routine
  121.             (    struct TCPiopb*            iopb                        //    Parameter block
  122.             );
  123.  
  124. enum
  125. {                                                                                    //    Validity bit flags
  126.     timeoutValue=        0x80,
  127.     timeoutAction=    0x40,
  128.     typeOfService=    0x20,
  129.     precedence=            0x10
  130. };
  131. enum
  132. {                                                                                    //    TOS flags
  133.     lowDelay=             0x01,
  134.     throughPut=            0x02,
  135.     reliability=        0x04
  136. };
  137.  
  138. /*=====================*/
  139. /*===---------------===*/
  140.  
  141. typedef struct HistoBucket
  142. {                                                                                    //    A histogram bucket
  143.     unsigned short    value;                                        //    
  144.     unsigned long        counter;                                    //    Number of buckets
  145. };
  146.  
  147. /*===---------------===*/
  148.  
  149. typedef struct TCPConnectionStats
  150. {                                                                                    //    Connection statistics
  151.     unsigned long        dataPktsRcvd;                            //    Packets received
  152.     unsigned long        dataPktsSent;                            //    Packets sent
  153.     unsigned long        dataPktsResent;                        //    Packets resent
  154.     unsigned long        bytesRcvd;                                //    Byte flow info
  155.     unsigned long        bytesRcvdDup;                            //    
  156.     unsigned long        bytesRcvdPastWindow;            //    
  157.     unsigned long        bytesSent;                                //
  158.     unsigned long        bytesResent;                            //
  159.     unsigned short    numHistoBuckets;                    //    Number of histogram buckets
  160.     struct HistoBucket    sentSizeHisto[NumOfHistoBuckets];
  161.     unsigned short    lastRTT;                                    //    
  162.     unsigned short    tmrSRTT;                                    //
  163.     unsigned short    rttVariance;                            //
  164.     unsigned short    tmrRTO;                                        //
  165.     byte                        sendTries;                                //    
  166.     byte                        sourchQuenchRcvd;                    //
  167. }TCPConnectionStats;
  168.  
  169. /*===---------------===*/
  170.  
  171. typedef struct TCPParam
  172. {                                                                                    //    TCP parameters
  173.     unsigned long        tcpRtoA;
  174.     unsigned long        tcpRtoMin;
  175.     unsigned long        tcpRtoMax;
  176.     unsigned long        tcpMaxSegSize;
  177.     unsigned long        tcpMaxConn;
  178.     unsigned long        tcpMaxWindow;
  179. }TCPParam;
  180.  
  181. /*===---------------===*/
  182.  
  183. typedef struct TCPStats
  184. {                                                                                    //    TCP statistics
  185.     unsigned long        tcpConnAttempts;
  186.     unsigned long        tcpConnOpened;
  187.     unsigned long        tcpConnAccepted;
  188.     unsigned long        tcpConnClosed;
  189.     unsigned long        tcpConnAborted;
  190.     unsigned long     tcpOctetsIn;
  191.     unsigned long     tcpOctetsOut;
  192.     unsigned long     tcpOctetsInDup;
  193.     unsigned long     tcpOctetsRetrans;
  194.     unsigned long     tcpInputPkts;
  195.     unsigned long     tcpOutputPkts;
  196.     unsigned long     tcpDupPkts;
  197.     unsigned long     tcpRetransPkts;
  198. }TCPStats;
  199.  
  200. /*===---------------===*/
  201. /*=====================*/
  202. /*===---------------===*/
  203.         
  204. typedef struct TCPCreatePB
  205. {                                                                                    //    TCPCreate parameter block
  206.     Ptr                            rcvBuff;                                    //    Receive buffer
  207.     unsigned long        rcvBuffLen;                                //    Its size
  208.     TCPNotifyProc     notifyProc;                                //    Notify routine
  209.     Ptr                            userDataPtr;                            //    Custom data
  210. }TCPCreatePB;    
  211.  
  212. /*===---------------===*/
  213.         
  214. typedef struct TCPOpenPB
  215. {                                                                                    //    TCPActiveOpen and TCPPassiveOpen parameter block
  216.     byte                        ulpTimeoutValue;                    //    Timeout span
  217.     byte                        ulpTimeoutAction;                    //    What to do then?
  218.     byte                        validityFlags;                        //    See above
  219.     byte                        commandTimeoutValue;            //
  220.     ip_addr                    remoteHost;                                //    Our target host
  221.     tcp_port                remotePort;                                //    Port we use for connection
  222.     ip_addr                    localHost;                                //    Local machine
  223.     tcp_port                localPort;                                //    Local port for connection
  224.     byte                        tosFlags;                                    //    See above
  225.     byte                        precedence;                                //
  226.     Boolean                    dontFrag;                                    //    Don't fragment flag
  227.     byte                        timeToLive;                                //    Time remaining to succeed
  228.     byte                        security;                                    //
  229.     byte                        optionCnt;                                //    Number of options to follow
  230.     byte                        options[40];                            //
  231.     Ptr                            userDataPtr;                            //    Custom data
  232. }TCPOpenPB;
  233.  
  234. /*===---------------===*/
  235.  
  236. typedef struct TCPSendPB
  237. {                                                                                    //    TCPSend parameter block
  238.     byte                        ulpTimeoutValue;                    //    Timeout span
  239.     byte                        ulpTimeoutAction;                    //    What to do then?
  240.     byte                        validityFlags;                        //    See above
  241.     Boolean                    pushFlag;                                    //    Push data through?
  242.     Boolean                    urgentFlag;                                //    Is this send urgent?
  243.     Ptr                            wdsPtr;                                        //    Write pointer
  244.     unsigned long     sendFree;                                    //
  245.     unsigned short     sendLength;                                //
  246.     Ptr                            userDataPtr;                            //    Custom data
  247. }TCPSendPB;
  248.  
  249. /*===---------------===*/
  250.  
  251. typedef struct TCPReceivePB
  252. {                                                                                    //    TCPRcv, TCPBrfReturn, and TCPNoCopyRcv parameter block
  253.     byte                        commandTimeoutValue;            //    Timeout span
  254.     byte                        filler;                                        //    Unneeded
  255.     Boolean                    markFlag;                                    //
  256.     Boolean                    urgentFlag;                                //    Urgent action?
  257.     Ptr                            rcvBuff;                                    //    Receive buffer
  258.     unsigned short    rcvBuffLen;                                //    Its length
  259.     Ptr                            rdsPtr;                                        //    Read pointer
  260.     unsigned short    rdsLength;                                //    Its length
  261.     unsigned short    secondTimeStamp;                    //
  262.     Ptr                            userDataPtr;                            //    Custom data
  263. }TCPReceivePB;
  264.  
  265. /*===---------------===*/
  266.  
  267. typedef struct TCPClosePB
  268. {                                                                                    //    TCPClose parameter block
  269.     byte                        ulpTimeoutValue;                    //    Timeout span
  270.     byte                        ulpTimeoutAction;                    //    What to do then?
  271.     byte                        validityFlags;                        //    See above
  272.     Ptr                            userDataPtr;                            //    Custom data
  273. }TCPClosePB;
  274.  
  275. /*===---------------===*/
  276.  
  277. typedef struct TCPStatusPB
  278. {                                                                                    //    TCPStatus parameter block
  279.     byte                        ulpTimeoutValue;                    //    Timeout span
  280.     byte                        ulpTimeoutAction;                    //    What to do then?
  281.     long                        unused;
  282.     ip_addr                    remoteHost;                                //    Our target host
  283.     tcp_port                remotePort;                                //    Port we use for connection
  284.     ip_addr                    localHost;                                //    Local machine
  285.     tcp_port                localPort;                                //    Local port for connection
  286.     byte                        tosFlags;                                    //    Type of service
  287.     byte                        precedence;                                //
  288.     byte                        connectionState;                    //
  289.     unsigned short    sendWindow;                                //    Communication windows
  290.     unsigned short    rcvWindow;                                //
  291.     unsigned short    amtUnackedData;                        //    Amount of unacknowledged data
  292.     unsigned short    amtUnreadData;                        //    Amount of unread data
  293.     Ptr                            securityLevelPtr;                    //
  294.     unsigned long        sendUnacked;                            //    Send unacknowledged data
  295.     unsigned long        sendNext;                                    //    
  296.     unsigned long     congestionWindow;                    //
  297.     unsigned long     rcvNext;                                    //
  298.     unsigned long     srtt;                                            //    Smoothed round-trip time (ms)
  299.     unsigned long     lastRTT;                                    //    Last round-trip time (ms)
  300.     unsigned long     sendMaxSegSize;                        //    Largest segment we can send
  301.     struct TCPConnectionStats*    connStatPtr;    //    Statistics block
  302.     Ptr                         userDataPtr;                            //    Custom data
  303. }TCPStatusPB;
  304.  
  305. /*===---------------===*/
  306.  
  307. typedef struct TCPAbortPB
  308. {                                                                                    //    TCPAbort parameter block
  309.     Ptr                         userDataPtr;                            //    Custom data
  310. }TCPAbortPB;
  311.  
  312. /*===---------------===*/
  313.  
  314. typedef struct TCPGlobalInfoPB
  315. {                                                                                    //    TCPGlobalInfo parameter block
  316.     struct TCPParam*    tcpParamPtr;                        //    Parameters
  317.     struct TCPStats*    tcpStatsPtr;                        //    Statistics
  318.     unsigned long*        tcpCDBTable;                        //
  319.     Ptr                             userDataPtr;                        //    Custom data
  320. }TCPGlobalInfoPB;
  321.  
  322. /*===---------------===*/
  323.  
  324. typedef struct ipctlGetAddrPB
  325. {
  326.     ip_addr                ourAddress;                                            //    Local IP address
  327.     long                    ourNetMask;                                            //    Local IP network mask
  328. }ipctlGetAddrPB;
  329.  
  330. /*===---------------===*/
  331.  
  332. typedef struct TCPiopb
  333. {                                                                                    //    TCP parameter block
  334.     struct QElem*            qLink;                                    //    Address of next queue element
  335.     short                            qType;                                    //    Always ioQType (2)
  336.     short                            ioTrap;                                    //    Used internally
  337.     Ptr                                ioCmdAddr;                            //    Used internally
  338.     ProcPtr                        ioCompletion;                        //    Completion routine
  339.     OSErr                         ioResult;                                //    Result code
  340.     StringPtr                    ioNamePtr;                            //    Used internally
  341.     short                         ioVRefNum;                            //    Used internally
  342.     short                            ioCRefNum;                            //
  343.     short                         csCode;                                    //    Command code
  344.     StreamPtr                     tcpStream;                            //    Our stream reference
  345.     union
  346.     {                                                                                    //    Specific tasks
  347.         TCPCreatePB            create;
  348.         TCPOpenPB                open;
  349.         TCPSendPB                send;
  350.         TCPReceivePB         receive;
  351.         TCPClosePB            close;
  352.         TCPAbortPB            abort;
  353.         TCPStatusPB            status;
  354.         TCPGlobalInfoPB    globalInfo;
  355.     } csParam;
  356. }TCPiopb, *TCPiopbPtr;
  357.  
  358. /*===---------------===*/
  359.  
  360. typedef struct IPParamBlock
  361. {                                                                                    //    TCP parameter block
  362.     struct QElem*            qLink;                                    //    Address of next queue element
  363.     short                            qType;                                    //    Always ioQType (2)
  364.     short                            ioTrap;                                    //    Used internally
  365.     Ptr                                ioCmdAddr;                            //    Used internally
  366.     ProcPtr                        ioCompletion;                        //    Completion routine
  367.     OSErr                         ioResult;                                //    Result code
  368.     StringPtr                    ioNamePtr;                            //    Used internally
  369.     short                         ioVRefNum;                            //    Used internally
  370.     short                            ioCRefNum;                            //
  371.     short                         csCode;                                    //    Command code
  372.     ip_addr                        ourAddress;                            //    Local address
  373.     long                            outnetMask;                            //    Local network mask
  374. }IPParamBlock, *IPParamBlockPtr;
  375.  
  376. /*===---------------===*/
  377. /*=====================*/
  378. /*===---------------===*/
  379.  
  380. struct CTCP : CMacTCPDriver
  381. Begin
  382. protected:
  383.     ip_addr                    remoteAddress;                        //    The IP address of the remote machine
  384.     ip_addr                    localAddress;                            //    The IP address of the local machine
  385.     ip_port                    destinationPort;                    //    The port number of the destination machine (usually zero)
  386.     ip_port                    sourcePort;                                //    The port number of the local machine
  387.     char                        remoteHostName[kNameLength];//Ascii name of the destination machine
  388.     unsigned long        streamID;                                    //    The connection ID for the stream
  389.     Size                        streamLength;                            //    Length of the stream
  390.     Ptr                            streamBuffer;                            //    Buffer for our stream
  391.     Ptr                            inBuffer;                                    //    The processed data
  392.     Ptr                            outBuffer;                                //    The outgoing data
  393.     Size                        inDataLength;                            //    How long the data is or can be
  394.     Size                         outDataLength;                        //    The outgoing data length
  395.     Boolean                    fMark;                                        //    The mark flag
  396.     Boolean                    fUrgent;                                    //    The urgent flag
  397.     byte                        timeOutDelay;                            //    The amount of time that should be spent waiting
  398.     
  399. public:    
  400.     OSErr    ITCP                                                                //    Initialize this object
  401.             (    void                                                                    //    No arguments
  402.             );
  403.     
  404.     void    Dispose                                                            //    Get rid of our object
  405.             (    void                                                                    //    No arguments
  406.             );
  407.     
  408.     virtual OSErr    Create                                            //    Creates a stream and data buffers
  409.             (    void                                                                    //    No arguments
  410.             );
  411.     virtual OSErr PassiveOpen                                    //    Wait for a connection
  412.             (    void                                                                    //    No arguments
  413.             );
  414.     virtual OSErr    ActiveOpen                                    //    Initiate a connection
  415.             (    void                                                                    //    No arguments
  416.             );
  417.     virtual OSErr Send                                                //    Send our data over the connection
  418.             (    void                                                                    //    No arguments
  419.             );
  420.     virtual OSErr    NoCopyRcv                                        //    Read received data directly
  421.             (    Ptr                            rdsPtr,                                //    Our read buffer
  422.                 short                        rdsLength                            //    How much to read
  423.             );
  424.     virtual OSErr    BfrReturn                                        //    Return buffers after NoCopyRcv
  425.             (    Ptr                            rdsPtr                                //    Our read buffer
  426.             );
  427.     virtual OSErr Rcv                                                    //    Read a copy of data received over our connection
  428.             (    void                                                                    //    No arguments
  429.             );
  430.     virtual OSErr Close                                                //    No more data to send, does not break connection
  431.             (    void                                                                    //    No arguments
  432.             );
  433.     virtual OSErr Abort                                                //    Terminate connection and disregard pending data
  434.             (    void                                                                    //    No arguments
  435.             );
  436.     virtual OSErr Release                                            //    Close the TCP stream and release buffers
  437.             (    void                                                                    //    No arguments
  438.             );
  439.     virtual OSErr    Status                                            //    Fetch connection information
  440.             (    TCPStatusPB*        theStatus                            //    The status record
  441.             );
  442.     virtual OSErr    GlobalInfo                                    //    Collect global statistics and parameters
  443.             (    TCPParam*                tcpParam,                            //    Parameter record
  444.                 TCPStats*                tcpStat                                //    Statistics record
  445.             );
  446.     virtual ip_addr GetMyIPAddress                        //    Fetches local IP#
  447.             (    void                                                                    //    No arguments
  448.             );
  449.     virtual void    MakeStreamBuffer                        //    Set up a stream buffer (forget previous)
  450.             (    Size                    length                                    //    Size of the buffer
  451.             );
  452.     virtual void    PlaceData                                        //    Set up out-going data and buffer
  453.             (    Ptr                        data,                                        //    Our data
  454.                 Size                    length                                    //    Size of the data field
  455.             );
  456.     virtual void    CreateInputBuffer                        //    Set up in-coming data and buffer
  457.             (    Ptr*                        data,                                    //    Our data buffer
  458.                 Size                        length                                //    Size of the data field
  459.             );
  460.     virtual OSErr    SetInputBuffer                            //    Set up in-coming data and buffer
  461.             (    Ptr                            data,                                    //    Our data buffer
  462.                 Size                        length                                //    Size of the data field
  463.             );
  464.     virtual Size    GetInputLength                            //    Get size of the input buffer
  465.             (    void                                                                    //    No arguments
  466.             );
  467.     virtual Ptr    GetInputBuffer                                //    Get the input buffer
  468.             (    void                                                                    //    No arguments
  469.             );
  470.     virtual OSErr    GetLastError                                //    Get our last error
  471.             (    void                                                                    //    No arguments
  472.             );
  473.     virtual void    SetDestination                            //    Set remote host parameters
  474.             (    char*                        name,                                    //    Remote machine's name
  475.                 ip_addr                    address,                            //    Remote machine's address
  476.                 ip_port                    port                                    //    Remote machine's port
  477.             );
  478.     virtual void    GetDestination                            //    Get remote host parameters
  479.             (    char*                        name,                                    //    Remote machine's name
  480.                 ip_addr*                address,                            //    Remote machine's address
  481.                 ip_port*                port                                    //    Remote machine's port
  482.             );
  483.     virtual void ReleaseStreamBuffer                        //    Releases the stream buffer
  484.             (    void                                                                    //    No arguments
  485.             );
  486.     virtual void ReleaseInputBuffer                        //    Releases the input buffer
  487.             (    void                                                                    //    No arguments
  488.             );
  489.     virtual void ReleaseOutputBuffer                        //    Releases the output buffer
  490.             (    void                                                                    //    No arguments
  491.             );
  492.     virtual void ReleaseBuffers                                //    Releases all our buffer
  493.             (    void                                                                    //    No arguments
  494.             );    
  495.     virtual void    SetTimeOut                                    //    Sets time out delay
  496.             (    byte                        timeOut                                //    The delay
  497.             );
  498.     virtual byte    GetTimeOut                                    //    Gets time out delay
  499.             (    void                                                                    //    No arguments
  500.             );
  501.  
  502. protected:
  503.     virtual void Wait                                                    //    Waits...
  504.             (    void                                                                    //    No arguments
  505.             );
  506.     virtual void SetSizeOfParameters                    //    Sets the size of the parameter block
  507.             (    void                                                                    //    No arguments
  508.             );
  509. End;
  510.  
  511. /*===---------------===*/
  512. /*=====================*/